package com.piano.moguyun.act;

import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.piano.moguyun.MainActivity;
import com.piano.moguyun.R;
import com.piano.moguyun.bean.LoginBean;
import com.piano.moguyun.bean.SendReportJson;
import com.piano.moguyun.bean.SendSaveJson;
import com.piano.moguyun.bean.SingBean;
import com.piano.moguyun.utils.DataUt;
import com.piano.moguyun.utils.HttpPostData;
import com.piano.moguyun.utils.IBaseAct;
import com.piano.moguyun.utils.LogUtils;
import com.piano.moguyun.utils.ToastUtils;

import java.net.URL;

import okhttp3.RequestBody;

/**
 * 签到
 */
public class SignAct extends IBaseAct implements View.OnClickListener {
    private LinearLayout ivBack;
    private TextView tvTitle;
    private EditText etDescription;//备注
    private EditText etLongitude;//经度
    private EditText etLatitude;//纬度
    private EditText etCity;//所在城市
    private EditText etProvince;//所在城市
    private EditText etAddress;//详细地址
    private EditText etType;
    private TextView tvSub;//提交
    private TextView tvJoin;//加入


    private String sign;//
    private String token;
    private String userId;
    private String type;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        statusBarBgColorId = R.color.color_F4F5F9;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_sign);
        userId = getIntent().getStringExtra("userId");
        token = getIntent().getStringExtra("token");
        LogUtils.e(TAG,"userId-------->"+userId);
        LogUtils.e(TAG,"token-------->"+token);
        initView();


    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        tvTitle = findViewById(R.id.tvTitle);
        etDescription = findViewById(R.id.etDescription);
        etLongitude = findViewById(R.id.etLongitude);
        etLatitude = findViewById(R.id.etLatitude);
        etCity = findViewById(R.id.etCity);
        etProvince = findViewById(R.id.etProvince);
        etAddress = findViewById(R.id.etAddress);
        tvJoin = findViewById(R.id.tvJoin);

        etType = findViewById(R.id.etType);
        tvSub = findViewById(R.id.tvSub);
        tvTitle.setText("签到");
        ivBack.setOnClickListener(this::onClick);
        tvSub.setOnClickListener(this::onClick);
        tvJoin.setOnClickListener(this::onClick);
    }

    /**
     * 签到
     */
    private void userSave() {
        String planId = getIntent().getStringExtra("planId");
        LogUtils.e(TAG,"planId-------->"+planId);
        if (TextUtils.isEmpty(planId)){
            ToastUtils.toast("planId数据异常",3);
            return;
        }
        type = etType.getText().toString().trim().equals("上班") ? "START" : "END";
        //planId = "a8206f97124c5a92b6ad3e73a151f6b6";
        sign = HttpPostData.getSign(type,planId,userId,etAddress.getText().toString().trim());
        LogUtils.e(TAG,"sign-------->"+sign);
        if (TextUtils.isEmpty(sign)){
            ToastUtils.toast("sign数据异常",3);
            return;
        }
        SendSaveJson saveJson2 = new SendSaveJson();
        saveJson2.setDevice("Android");
        saveJson2.setAddress(etAddress.getText().toString().trim());
        saveJson2.setT("");
        saveJson2.setDescription(etDescription.getText().toString().trim());
        saveJson2.setCountry("中国");
        saveJson2.setLongitude(etLongitude.getText().toString().trim());
        saveJson2.setCity(etCity.getText().toString().trim());
        saveJson2.setLatitude(etLatitude.getText().toString().trim());
        saveJson2.setPlanId(planId);
        saveJson2.setProvince(etProvince.getText().toString().trim());
        saveJson2.setType(type);
        LogUtils.e(TAG,"saveJson2------>"+new Gson().toJson(saveJson2));
        RequestBody body = RequestBody.create(DataUt.JSON,new Gson().toJson(saveJson2));

        OkGo.<String>post(DataUt.saveUri)
                .upRequestBody(body)
                .headers(HttpPostData.getHeader(sign,token))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e(TAG,"response------>"+response.body());
                       SingBean singBean = new Gson().fromJson(response.body(), SingBean.class);
                        if (singBean != null) {
                            int code = singBean.getCode();
                            if (code == 200) {
                                ToastUtils.toast("签到成功", 1);
                            }else{
                                ToastUtils.toast(singBean.getMsg(), 2);
                            }
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {

        Uri  uri = Uri.parse("https://jq.qq.com/?_wv=1027&k=auq1jOxK");
        Intent intent = new Intent(Intent.ACTION_VIEW,uri);

        startActivity(intent);

        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvSub://签到
                if (TextUtils.isEmpty(etDescription.getText().toString().trim())){
                    ToastUtils.toast("请输入打卡备注",3);
                    return;
                }
                if (TextUtils.isEmpty(etLongitude.getText().toString().trim())){
                    ToastUtils.toast("请输入位置经度",3);
                    return;
                }
                if (TextUtils.isEmpty(etLatitude.getText().toString().trim())){
                    ToastUtils.toast("请输入位置纬度",3);
                    return;
                }
                if (TextUtils.isEmpty(etCity.getText().toString().trim())){
                    ToastUtils.toast("请输入所在城市",3);
                    return;
                }
                if (TextUtils.isEmpty(etProvince.getText().toString().trim())){
                    ToastUtils.toast("请输入所在省份",3);
                    return;
                }
                if (TextUtils.isEmpty(etAddress.getText().toString().trim())){
                    ToastUtils.toast("请输入详细地址",3);
                    return;
                }
                if (TextUtils.isEmpty(etType.getText().toString().trim())){
                    ToastUtils.toast("请输入上班或者下班",3);
                    return;
                }
                userSave();
                break;
        }


    }


}
